package com.yuyang.gulimall.ware.service.impl;

import com.alibaba.fastjson.TypeReference;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yuyang.common.utils.PageUtils;
import com.yuyang.common.utils.Query;
import com.yuyang.common.utils.R;
import com.yuyang.gulimall.ware.dao.WareInfoDao;
import com.yuyang.gulimall.ware.entity.WareInfoEntity;
import com.yuyang.gulimall.ware.feign.MemberFeignService;
import com.yuyang.gulimall.ware.service.WareInfoService;
import com.yuyang.gulimall.ware.vo.FareVo;
import com.yuyang.gulimall.ware.vo.MemberAddressVo;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Map;


@Service("wareInfoService")
public class WareInfoServiceImpl extends ServiceImpl<WareInfoDao, WareInfoEntity> implements WareInfoService {
    @Autowired
    MemberFeignService memberFeignService;
    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        LambdaQueryWrapper<WareInfoEntity> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        String key = (String) params.get("key");
        lambdaQueryWrapper.and(StringUtils.isNotBlank(key), wrapper ->
                wrapper.eq(WareInfoEntity::getId, key).
                or().like(WareInfoEntity::getName, key).
                or().like(WareInfoEntity::getAddress, key).
                or().like(WareInfoEntity::getAreacode, key));

        IPage<WareInfoEntity> page = this.page(
                new Query<WareInfoEntity>().getPage(params),
                lambdaQueryWrapper
        );
        return new PageUtils(page);
    }

    @Override
    public FareVo getFare(Long addrId) {
        FareVo fareVo = new FareVo();
        R infoById = memberFeignService.getInfoById(addrId);
        MemberAddressVo data = infoById.getData(new TypeReference<MemberAddressVo>() {
        });
        fareVo.setAddress(data);
        fareVo.setFare(new BigDecimal(data.getPhone().substring(data.getPhone().length()-1)));
        return fareVo;
    }

}